﻿/**
 * Discription: 
 * File Name:   $ItemName$.cs
 * Author   :   陈小磊(Gtalk/Email:chenlei@hzexe.com)
 * Create Time: 2013/12/16 13:13:35
 * CLR Version: 4.0.30319.18052
 * File Identification: a2029181-b240-4f2a-b57c-253824183451
 * Create by wpc\machinename in project named "$ProjectName$" 
 * =================================================
 * Modify Records
 *          
 * 
 * 
 * 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Reflection;
using Newtonsoft.Json.Linq;

namespace Hzexe.Wechat.SharpSDK.Message.Push
{
    /// <summary>
    /// PushMsgConverter
    /// </summary>
    class PushMsgConverter : JsonConverter 
    {

        public override bool CanConvert(Type objectType)
        {
            var cs=objectType.GetCustomAttributes(typeof(MyJsonProperty),false);
            return cs.Length>0;
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
                JObject obj = JObject.FromObject(value);
                var newname = obj["msgtype"].ToString();
                obj.Add(newname, obj["data"]);
                obj.Remove("data");
                serializer.Serialize(writer, obj);
        }
    }
}
